package com.lhd.service;

import com.lhd.dao.CommentDao;
import com.lhd.po.Comment;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by limi on 2017/10/22.
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;

    @Override
    public List<Comment> listCommentByBlogId(Long blogId) {
        //Sort sort = new Sort("createTime");
        List<Comment> comments = commentDao.findTopParentCommentByBlogId(blogId);
        for (Comment comment:comments){
            List<Comment> comm = commentDao.findByBlogIdAndTopParentCommentId(blogId, comment.getId());
            for (Comment com:comm){
                com.setParentComment(commentDao.getCommentByCommentId(com.getParentCommentId()));
            }
            comment.setReplyComments(comm);
        }
        //return eachComment(comments);//@todo
        return comments;
    }

    @Transactional
    @Override
    public Integer saveComment(Comment comment) {

        //pi = -1 则为顶级父节点，否则是父节点的id
        //ti = -1 则为顶级父节点，此时pi也为-1，否则是顶级父节点id

        //ti = -1   pi = -1  超级父节点
        //ti != -1   pi = -1 不是超级父节点，ti是超级
        //ti != -1   pi != -1 不是超级父节点，ti是超级

        Long parentCommentId = comment.getParentCommentId();
        if (parentCommentId==null){ //这条消息为顶级父节点，pi ti皆为-1
            comment.setParentCommentId(-1l);
            comment.setTopParentCommentId(-1l);
        }else {
            comment.setParentCommentId(parentCommentId);
            //获取父节点
            Comment parentComment = commentDao.getCommentByCommentId(parentCommentId);
            //判断该节点是不是顶级父节点
            if (parentComment.getTopParentCommentId() == -1){ //该节点是超级父节点
                comment.setTopParentCommentId(parentCommentId);
            }else {
                comment.setTopParentCommentId(parentComment.getTopParentCommentId());
            }
        }
        return commentDao.save(comment);//@todo
    }


    /**
     * 循环每个顶级的评论节点
     * @param comments
     * @return
     */
    private List<Comment> eachComment(List<Comment> comments) {
        List<Comment> commentsView = new ArrayList<>();
        for (Comment comment : comments) {
            Comment c = new Comment();
            BeanUtils.copyProperties(comment,c);
            commentsView.add(c);
        }
        //合并评论的各层子代到第一级子代集合中
        combineChildren(commentsView);
        return commentsView;
    }

    /**
     *
     * @param comments root根节点，blog不为空的对象集合
     * @return
     */
    private void combineChildren(List<Comment> comments) {

        for (Comment comment : comments) {
            List<Comment> replys1 = comment.getReplyComments();
            for(Comment reply1 : replys1) {
                //循环迭代，找出子代，存放在tempReplys中
                recursively(reply1);
            }
            //修改顶级节点的reply集合为迭代处理后的集合
            comment.setReplyComments(tempReplys);
            //清除临时存放区
            tempReplys = new ArrayList<>();
        }
    }

    //存放迭代找出的所有子代的集合
    private List<Comment> tempReplys = new ArrayList<>();
    /**
     * 递归迭代，剥洋葱
     * @param comment 被迭代的对象
     * @return
     */
    private void recursively(Comment comment) {
        tempReplys.add(comment);//顶节点添加到临时存放集合
        if (comment.getReplyComments().size()>0) {
            List<Comment> replys = comment.getReplyComments();
            for (Comment reply : replys) {
                tempReplys.add(reply);
                if (reply.getReplyComments().size()>0) {
                    recursively(reply);
                }
            }
        }
    }
}
